package com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.dto.ErpSalesreturnsDto;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.dto.SaveErpSalesreturnsFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.vo.ErpSalesreturnsFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.vo.ErpSalesreturnsListVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.dto.ErpSalesreturnsListDto;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.vo.ErpSalesreturnsentryVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.entity.ErpSalesreturnsentry;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.dto.ErpSalesreturnsentryDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.excel.entity.XjrExcelImport;
import com.xjrsoft.module.excel.factory.ParserFactory;
import com.xjrsoft.module.excel.parser.ImportDataParser;
import com.xjrsoft.module.excel.service.IXjrExcelImportService;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.entity.ErpSalesreturns;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.vo.ErpSalesreturnsVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.service.IErpSalesreturnsService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_salesreturns")
@Api(value = "销售退货", tags = "销售退货接口")
public class ErpSalesreturnsController {

	private final IXjrExcelImportService importService;

	private final ObjectMapper objectMapper;

	private final IErpSalesreturnsService erpSalesreturnsService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpSalesreturns")
	public Response<ErpSalesreturnsFormDataVo> getFormData(@PathVariable String id) {
		ErpSalesreturnsFormDataVo formDataVo = new ErpSalesreturnsFormDataVo();
		// 主表数据
		ErpSalesreturns erpSalesreturns = erpSalesreturnsService.getById(id);
		formDataVo.setErpSalesreturnsVo(BeanUtil.copy(erpSalesreturns, ErpSalesreturnsVo.class));
		// 从表数据
		formDataVo.setErpSalesreturnsentryVoList(erpSalesreturnsService.getEntryInfo(erpSalesreturns.getFSalesoutbandid()));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpSalesreturns")
	public Response<PageOutput<ErpSalesreturnsListVo>> getPageList(ErpSalesreturnsListDto listDto) {
		IPage<ErpSalesreturns> page = erpSalesreturnsService.getPageList(listDto);
		List<ErpSalesreturnsListVo> records = BeanUtil.copyList(page.getRecords(), ErpSalesreturnsListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		//FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpSalesreturns")
	public Response save(@RequestBody SaveErpSalesreturnsFormDataDto formDto) {
		ErpSalesreturnsDto erpSalesreturnsDto = formDto.getErpSalesreturnsDto();
		ErpSalesreturns erpSalesreturns = BeanUtil.copy(erpSalesreturnsDto, ErpSalesreturns.class);
		List<ErpSalesreturnsentryDto> erpSalesreturnsentryDtoList = formDto.getErpSalesreturnsentryDto();
		List<ErpSalesreturnsentry> erpSalesreturnsentryList = BeanUtil.copyList(erpSalesreturnsentryDtoList, ErpSalesreturnsentry.class);
		String dataId = erpSalesreturnsService.addErpSalesreturns(erpSalesreturns, erpSalesreturnsentryList,erpSalesreturnsDto,erpSalesreturnsentryDtoList);
		if (dataId != null) {
			codeRuleService.useEncode("return");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpSalesreturns")
	public Response update(@PathVariable String id, @RequestBody SaveErpSalesreturnsFormDataDto formDto) {
		ErpSalesreturnsDto erpSalesreturnsDto = formDto.getErpSalesreturnsDto();
		ErpSalesreturns erpSalesreturns = BeanUtil.copy(erpSalesreturnsDto, ErpSalesreturns.class);

		List<ErpSalesreturnsentryDto> erpSalesreturnsentryDtoList = formDto.getErpSalesreturnsentryDto();
		List<ErpSalesreturnsentry> erpSalesreturnsentryList = BeanUtil.copyList(erpSalesreturnsentryDtoList, ErpSalesreturnsentry.class);
		return Response.status(erpSalesreturnsService.updateErpSalesreturns(id, erpSalesreturns, erpSalesreturnsentryList,erpSalesreturnsDto,erpSalesreturnsentryDtoList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpSalesreturnsService.removeByIds(Func.toStrList(ids)));
	}

	
	/**
	 * excel批量导入数据
	 */
	@PostMapping("/import")
	@ApiOperation(value = "excel批量导入数据", notes = "传入菜单id")
	public Response importData(@RequestParam("F_ModuleId") String moduleId, MultipartFile file) {
		XjrExcelImport excelImport = importService.getByModuleId(moduleId);
		ImportDataParser parser = ParserFactory.getImportDataParser();
		File excelFile = IoUtil.toFile(file);
		List<Map<String, Object>> recordList = parser.parseDataToMap(excelImport, excelFile);
		if (CollectionUtil.isNotEmpty(recordList)) {
			List<ErpSalesreturnsDto> entityDtoList = recordList.stream().map(record -> {
				try {
					return objectMapper.readValue(objectMapper.writeValueAsString(record), ErpSalesreturnsDto.class);
				} catch (JsonProcessingException e) {
				}
				return null;
			}).collect(Collectors.toList());
			List<ErpSalesreturns> entities = BeanUtil.copyList(entityDtoList, ErpSalesreturns.class);
			return Response.status(erpSalesreturnsService.saveBatch(entities));
		}
		return Response.notOk();
	}
}
